#!/usr/bin/python
#	-*-	coding:	UTF-8	-*-

import	cgi
import	os
import	cgitb
import	sqlite3
import	time
from	deuzenne	import	*

def	pywalletlogo():
	print	"<p>"
	print   '<div id="demo-container">'
	print	"PYWallet	-	Version	0.15a ("
	print	"<i>%s</i>)"	%	cgi.escape(os.environ["REMOTE_ADDR"])
	print	"</p>"
	print 	'<ul id="simple-menu">'
	print 	'<li><a href="pywallet.py?action=main" title="Retourne a la page d\'accueil" class="current" >Home</a></li>'
	print 	'<li><a href="pywallet.py?action=saisie" title="">Nouv. Op&eacute;ration</a></li>'
	print 	'<li><a href="pywallet.py?=action=config" title="">Configuration</a></li>'
	print 	'</ul>'
	print 	'</div>'
	
def	html_header():
	print	'<!DOCTYPE html PUBLIC	"-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
	print	'<html xmlns="http://www.w3.org/1999/xhtml"	xml:lang="fr" lang="fr">'
	print	'<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/><title>PYWallet</title>'
	print	'<link rel="stylesheet" media="screen" href="../walldesign/pyw.css"/>'
	print	"</head>"
	print	"<body>"

def	html_footer():
	
	print	"<br/></body></html>"

def	soldes(compte=1):
	dbconnect	=	sqlite3.connect("pywallet-dev.db")
	dbconnect.row_factory	=	sqlite3.Row
	db	=	dbconnect.cursor()
	db2	=	dbconnect.cursor()
	requete	=	'select	C.COMPTE_PKEY	cpkey,	C.NOM	nom	,	sum(M.MONTANT)	montant	FROM	comptes	C,	mouvements	M	WHERE	C.compte_pkey=M.compte	group	by	M.COMpte'
	db.execute	("%s"	%	requete);
	print	"	<TABLE	BORDER><CAPTION>Comptes	Courants</CAPTION>"
	print	"<TR><TH>	COMPTE	</TH>	<TH>	SOLDE	</TH></TR>"
	for	res	in	db:
		#print	"<TR><TD>%s</TD>	<TD>%s	EUR</TD></TR>"	%	(res['nom'],	res['montant'])
		print	'<TR><TD><a	href="pywallet.py?action=detail&compte=%s">%s</a></TD><TD>%s	EUR</TD></TR>'	%	(res['cpkey'],res['nom'],	res['montant'])
		
		#https://perso.gaudal.net/pywallet/pywallet.py?action=detail
		#	<a	href="http://www.startyourdev.com"	title="DevStarter"	target="_blank">StarYourDev</a>	
	print	"</TABLE>"
		
	db.close()

def	detail(compte=1):
	dbconnect	=	sqlite3.connect("pywallet-dev.db")
	dbconnect.row_factory	=	sqlite3.Row
	db	=	dbconnect.cursor()
	db2	=	dbconnect.cursor()
	requete	=	'select	date,libelle,montant	from	mouvements	where	compte=%s	order	by	date	desc	limit	20'	%	compte
	db.execute	("%s"	%	requete);
	print	"	<TABLE	BORDER><CAPTION>Dernieres	Operations	</CAPTION>"
	print	"<TR><TH>	DATE	</TH>	<TH>	LIBELLE	</TH>	<TH>	DEBIT	</TH>	<TH>	CREDIT	</TH></TR>"
	for	res	in	db:
	
		print	"<TR><TD>%s</TD>	<TD>%s</TD>"	%	(res['date'],	res['libelle'])
		if	res['montant']>0:
			print "<td></td><td>	%s	EUR</td></TR>"	%	(res['montant'])
		else:
			print "<td>	%s	EUR</td><td></td></TR>"	%	(res['montant'])
			
	print	"</TABLE>"
		
	db.close()

def saisie():
# <form name="input" action="html_form_action.asp" method="get">
# Username: <input type="text" name="user" />
# <input type="submit" value="Submit" />
# </form> 
	print '<form name="saisie" action"pywallet.py" methode="get">'
	print '<select name="compte">'
	print '<option value="CC - ING DIRECT" selected>1</option>'
	print '<option value="Physics">2</option>'
	print '</select>'
	print 'First name: <input type="text" name="firstname" /><br />'
	print 'Last name: <input type="text" name="lastname" /><br/>'
	print '<input type="submit" value="Submit">'
	print '</form>'
	
def	main():		
	cgitb.enable(display=1)

	print "Content-Type: text/html"
	print ""
	html_header()
	pywalletlogo()
	ip_src=cgi.escape(os.environ["REMOTE_ADDR"])
	if	(ip_src	=='195.254.158.41') or (ip_src=='192.168.25.254'):
		form=cgi.FieldStorage()
		action=form.getvalue("action","main")
		compte=form.getvalue("compte","1")
		# print 'action	%s'	%	action
		if	action=="main":
			soldes()
		elif action=="detail":
			detail(compte)
		elif action=="saisie":
			soldes()
			print "<br/>"
			saisie()
	# print	form.getvalue('url_parameter')
	html_footer()	
	
main()

